package com.qianbao.qianbaobusiness.ui.cashier.databill.dialog;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.ui.widget.calendar.adapter.AbstractWheelTextAdapter;
import com.qianbao.qianbaobusiness.ui.widget.calendar.listener.OnWheelChangedListener;
import com.qianbao.qianbaobusiness.ui.widget.calendar.listener.OnWheelScrollListener;
import com.qianbao.qianbaobusiness.ui.widget.calendar.view.BaseDialog;
import com.qianbao.qianbaobusiness.ui.widget.calendar.view.WheelView;

import java.util.ArrayList;
import java.util.List;

import static com.qianbao.qianbaobusiness.ui.widget.calendar.bean.ItemsRange.getYear;

/**
 * 日期选择对话框
 * @author pengjian
 */
public class WheelPickerDialog extends BaseDialog implements View.OnClickListener {

    private static final int MIN_YEAR = 1970;
    private static final int MAX_YEAR = 2100;

    public static final int DIALOG_MODE_CENTER = 0;
    public static final int DIALOG_MODE_BOTTOM = 1;

    private Context context;
    private WheelView wvYear;

    private View vDialog;
    private View vDialogChild;
    private ViewGroup VDialogPicker;
    //    private TextView tvTitle;
    private TextView btnSure;
    private TextView btnCancel;

    private List<String> arry_years;
    private CalendarTextAdapter mYearAdapter;

    private int currentYear = getYear();

    private int maxTextSize = 24;
    private int minTextSize = 18;


    private String selectYear;

    //    private String strTitle = "选择日期";

    private OnDatePickListener onDatePickListener;

    public WheelPickerDialog(Context context, List<String> lists) {
        super(context, R.layout.wheel_dialog_picker_center);
        this.context = context;
        arry_years = lists;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        VDialogPicker = (ViewGroup) findViewById(R.id.ly_dialog_picker);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
        // 此处相当于布局文件中的Android:layout_gravity属性
        lp.gravity = Gravity.CENTER_VERTICAL;

        wvYear = new WheelView(context);
        wvYear.setLayoutParams(lp);
        VDialogPicker.addView(wvYear);


        vDialog = findViewById(R.id.ly_dialog);
        vDialogChild = findViewById(R.id.ly_dialog_child);
        //        tvTitle = (TextView) findViewById(R.id.tv_dialog_title);
        btnSure = (TextView) findViewById(R.id.btn_dialog_sure);
        btnCancel = (TextView) findViewById(R.id.btn_dialog_cancel);

        //        tvTitle.setText(strTitle);
        vDialog.setOnClickListener(this);
        vDialogChild.setOnClickListener(this);
        btnSure.setOnClickListener(this);
        if (null != btnCancel) {
            btnCancel.setOnClickListener(this);
        }

        mYearAdapter = new CalendarTextAdapter(context, arry_years, setYear(currentYear), maxTextSize, minTextSize);
        wvYear.setVisibleItems(5);
        wvYear.setViewAdapter(mYearAdapter);
        wvYear.setCurrentItem(setYear(currentYear));
        wvYear.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem());
                selectYear = currentText;
                setTextviewSize(currentText, mYearAdapter);
            }
        });

        wvYear.addScrollingListener(new OnWheelScrollListener() {

            @Override
            public void onScrollingStarted(WheelView wheel) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                // TODO Auto-generated method stub
                String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem());
                setTextviewSize(currentText, mYearAdapter);
            }
        });


    }

    public void setDialogMode(int dialogMode) {
        if (dialogMode == DIALOG_MODE_BOTTOM) {
            resetContent(R.layout.calendar_dialog_picker_bottom);
            setAnimation(R.style.AnimationBottomDialog);
            setGravity(Gravity.BOTTOM);
        }
    }

    @Override
    protected int dialogWidth() {
        DisplayMetrics metric = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric.widthPixels;
    }

    public void setDatePickListener(OnDatePickListener onDatePickListener) {
        this.onDatePickListener = onDatePickListener;
    }

    @Override
    public void onClick(View v) {
        if (v == btnSure) {
            if (onDatePickListener != null) {
                onDatePickListener.onClick(TextUtils.isEmpty(selectYear) ? arry_years.get(0) : selectYear);
            }
        } else if (v == btnCancel) {

        } else if (v == vDialogChild) {
            return;
        } else {
            dismiss();
        }
        dismiss();
    }

    public interface OnDatePickListener {
        public void onClick(String year);
    }

    /**
     * 设置字体大小
     * @param curriteItemText
     * @param adapter
     */
    public void setTextviewSize(String curriteItemText, CalendarTextAdapter adapter) {
        ArrayList<View> arrayList = adapter.getTestViews();
        int size = arrayList.size();
        String currentText;
        for (int i = 0; i < size; i++) {
            TextView textvew = (TextView) arrayList.get(i);
            currentText = textvew.getText().toString();
            if (curriteItemText.equals(currentText)) {
                textvew.setTextSize(maxTextSize);
            } else {
                textvew.setTextSize(minTextSize);
            }
        }
    }


    /**
     * 设置年份
     * @param year
     */
    private int setYear(int year) {
        int yearIndex = 0;
        for (int i = MIN_YEAR; i < MAX_YEAR; i++) {
            if (i == year) {
                return yearIndex;
            }
            yearIndex++;
        }
        return yearIndex;
    }


    private class CalendarTextAdapter extends AbstractWheelTextAdapter {
        List<String> list;

        protected CalendarTextAdapter(Context context, List<String> list, int currentItem, int maxsize, int minsize) {
            super(context, R.layout.calendar_item_birth_year, NO_RESOURCE, currentItem, maxsize, minsize);
            this.list = list;
            setItemTextResource(R.id.tempValue);
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            View view = super.getItem(index, cachedView, parent);
            return view;
        }

        @Override
        public int getItemsCount() {
            return list.size();
        }

        @Override
        protected CharSequence getItemText(int index) {
            return list.get(index) + "";
        }
    }

}